Public Class OrderList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dgOrders As System.Web.UI.WebControls.DataGrid
    Protected WithEvents PageSelect1 As SR.WebControls.PageSelect
    Protected WithEvents ddlCustomer As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtOrderNo As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnSearch As System.Web.UI.WebControls.Button
    Protected WithEvents lblTotal As System.Web.UI.WebControls.Label
    Protected WithEvents Pageselect2 As SR.WebControls.PageSelect
    Protected WithEvents lblTotalText As System.Web.UI.WebControls.Label
    Protected WithEvents comvOrderNo As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents ddlStatus As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Protected WithEvents Header1 As Header

    Private ORDER_INPUT_URL As String = "/Internal/OrderInput.aspx"
    Private PAGE_SIZE As Integer = 20


#Region "   Properties"
    Private ReadOnly Property ReturnURL() As String
        Get
            Return Server.UrlEncode(String.Format(Me.Request.Url.AbsolutePath & "?cid={0}&ono={1}", Me.ddlCustomer.SelectedItem.Value, Me.txtOrderNo.Text))
        End Get
    End Property
#End Region

#Region "   Grid binding fucntions"
    Protected Function BuildViewOrderLink(ByVal container As Object) As String
        Dim id As Integer = CInt(DataBinder.Eval(container, "DataItem.ORDER_ID_PK"))
        Dim ono As String = CStr(DataBinder.Eval(container, "DataItem.ORDER_NUMBER"))
        Dim url As String = AppRoot() & "/Customer/OrderView.aspx?id={0}&ru=" & ReturnURL
        Return String.Format("<a href=" & url & ">{1}</a>", id, ono)
    End Function

    'Protected Function BuildOrderStatusUpdateLink(ByVal container As Object) As String
    '    Dim id As Integer = CInt(DataBinder.Eval(container, "DataItem.ORDER_ID_PK"))
    '    Dim type As EOrderType = CType(DataBinder.Eval(container, "DataItem.ORDER_TYPE"), EOrderType)
    '    Dim status As EOrderStatus = CType(DataBinder.Eval(container, "DataItem.STATUS"), EOrderStatus)

    '    If status <> EOrderStatus.Active Then
    '        Return "N/A"
    '    End If

    '    If type = EOrderType.Bulk_Order Then
    '        Return "N/A"
    '    Else
    '        Dim dataExists As Boolean = True

    '        Try
    '            Dim objOS As New COrderStatus
    '            objOS.Load(id, Week(Now), Now.Year)
    '        Catch ex As DataNotExistsException
    '            dataExists = False
    '        Catch ex As Exception
    '        End Try

    '        Dim url As String = AppRoot() & "/Internal/OrderStatusDataFilling.aspx?id={0}&ru=" & ReturnURL
    '        Return String.Format("<a href=" & url & ">Update</a> {1}", id, IIf(dataExists, "", "(<font color=red>*</font>)"))
    '    End If
    'End Function

    Protected Function BuildAdjustQuantityLink(ByVal container As Object) As String
        Dim id As Integer = CInt(DataBinder.Eval(container, "DataItem.ORDER_ID_PK"))
        Dim status As EOrderStatus = CType(DataBinder.Eval(container, "DataItem.STATUS"), EOrderStatus)

        If status <> EOrderStatus.Active Then
            Return "N/A"
        End If

        Dim fixed As Boolean
        Dim objOrder As New COrder
        objOrder.Load(id)
        fixed = objOrder.IsAllQuantityFixed

        If fixed Then
            Return "N/A"
        Else
            Dim url As String = AppRoot() & "/Internal/AdjustQuantity.aspx?id={0}&ru=" & ReturnURL
            Return String.Format("<a href=" & url & ">Adjust</a>", id)
        End If
    End Function

    Protected Function BuildProductionStatusUpdateLink(ByVal container As Object) As String
        Dim id As Integer = CInt(DataBinder.Eval(container, "DataItem.ORDER_ID_PK"))
        Dim iPO As Boolean
        Dim status As EOrderStatus = CType(DataBinder.Eval(container, "DataItem.STATUS"), EOrderStatus)

        If status <> EOrderStatus.Active Then
            Return "N/A"
        End If

        Dim objOrder As New COrder
        objOrder.Load(id)
        iPO = objOrder.IsIndependentPO

        If Not iPO Then
            Return "N/A"
        Else
            Dim oi As COrderItem
            Dim dataExists As Boolean = True

            For Each oi In objOrder.OrderItems
                Dim objPS As New CProductionStatus
                Try
                    objPS.Load(oi.ID, Week(Now), Year(Now))
                Catch ex As DataNotExistsException
                    dataExists = False
                    Exit For
                Catch ex As Exception

                End Try
            Next

            Dim url As String = AppRoot() & "/Internal/ProductionStatusUpdate.aspx?id={0}&ru=" & ReturnURL
            Return String.Format("<a href=" & url & ">Update</a> {1}", id, IIf(dataExists, "", "(<font color=red>*</font>)"))
        End If
    End Function

#End Region

#Region "   Page functions"
    Private Sub BindGrid()
        'get order list 
        'return a dataset with 2 tables
        'the first one will contain order list
        'the first column of first row of the second one will contain total available order
        Dim objOrder As New COrder
        Dim ds As DataSet = objOrder.GetOrderList(CInt(Me.ddlCustomer.SelectedItem.Value), Me.txtOrderNo.Text, CByte(Me.ddlStatus.SelectedItem.Value), Me.PageSelect1.CurrentPageIndex * Me.PageSelect1.PageSize, Me.PageSelect1.PageSize)

        Me.dgOrders.DataSource = ds.Tables(0)
        Me.dgOrders.DataBind()

        Dim cnt As Integer = CInt(ds.Tables(1).Rows(0)(0))

        Me.lblTotal.Text = cnt.ToString

        'If cnt > 0 Then
        '    Me.lblTotalText.Text = "ORDERS"
        'Else
        '    Me.lblTotalText.Text = "ORDER"
        'End If


        'create top page select
        Me.PageSelect1.ItemCount = cnt
        Me.PageSelect1.Create()

        'create bottom page select and sync with top page select
        Me.Pageselect2.ItemCount = Me.PageSelect1.ItemCount
        Me.Pageselect2.Create()
    End Sub
#End Region
    

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Me.IsPostBack Then
            Security.InternalAndAdminOnly()

            Header1.NavigationBar.Add("Create new order", AppRoot() & ORDER_INPUT_URL)
            'Header1.MenuIndex = 0
            Header1.MenuIndex = EMenuIndex.InternalOrderList

            Me.ddlCustomer.DataSource = COrder.GetCustomerListFromOrders
            Me.ddlCustomer.DataTextField = "NAME"
            Me.ddlCustomer.DataValueField = "US_ID_PK"
            Me.ddlCustomer.DataBind()
            Me.ddlCustomer.Items.Insert(0, New ListItem("[All]", "0"))

            Me.ddlStatus.DataSource = EnumToList(GetType(EOrderStatus))
            Me.ddlStatus.DataTextField = "Text"
            Me.ddlStatus.DataValueField = "Value"
            Me.ddlStatus.DataBind()

            Try
                Me.ddlCustomer.SelectedIndex = Me.ddlCustomer.Items.IndexOf(ddlCustomer.Items.FindByValue(Me.Request.QueryString("cid")))
            Catch ex As Exception
                Me.ddlCustomer.SelectedIndex = 0
            End Try

            Try
                Me.txtOrderNo.Text = Request.QueryString("ono")
            Catch ex As Exception
                Me.txtOrderNo.Text = ""
            End Try


            'If ddlCustomer.Items.Count > 1 Then
            '    Me.PageSelect1.PageSize = Me.PAGE_SIZE
            '    Me.Pageselect2.PageSize = Me.PageSelect1.PageSize

            '    Me.BindGrid()
            'Else
            '    Me.PageSelect1.Visible = False
            '    Me.Pageselect2.Visible = False
            '    Me.lblTotal.Text = "0"
            '    'Me.lblTotalText.Text = "ORDER"
            'End If

            Me.PageSelect1.PageSize = Me.PAGE_SIZE
            Me.Pageselect2.PageSize = Me.PageSelect1.PageSize

            Me.BindGrid()

        End If
    End Sub

    Private Sub PageSelect1_PageIndexChanged(ByVal newPageIndex As Integer) Handles PageSelect1.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.Pageselect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Private Sub Pageselect2_PageIndexChanged(ByVal newPageIndex As Integer) Handles Pageselect2.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.Pageselect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.PageSelect1.CurrentPageIndex = 0
        Me.Pageselect2.CurrentPageIndex = 0
        Me.BindGrid()
    End Sub
End Class
